package com.flk.oauth;

import com.flk.oauth.entity.OauthAuthority;
import com.flk.oauth.entity.OauthRole;
import com.flk.oauth.entity.OauthUser;
import com.flk.oauth.repository.OauthAuthorityRepository;
import com.flk.oauth.repository.OauthRoleRepository;
import com.flk.oauth.service.IOauthUserService;
import com.google.common.collect.Sets;
import lombok.AllArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author xxwy
 * on 2018/10/15 0015
 */
@Component
@AllArgsConstructor
public class Init implements CommandLineRunner {
    private IOauthUserService userService;
    private OauthAuthorityRepository authorityRepository;
    private OauthRoleRepository roleRepository;
    @Override
    public void run(String... strings) throws Exception {
        OauthAuthority authority1 = new OauthAuthority();
        authority1.setId(1L);
        authority1.setName("显示行业专家");
        authority1.setValue("show_specialist");
        if(!authorityRepository.findById(1L).isPresent()) {
            //======================================权限=========================

            authorityRepository.save(authority1);

            OauthAuthority authority2 = new OauthAuthority();
            authority2.setId(2L);
            authority2.setName("显示版主");
            authority2.setValue("show_forum_admin");
            authorityRepository.save(authority2);

            //=======================================角色=====================================
            //社圈
            OauthRole specialist = new OauthRole();
            specialist.setId(1L);
            specialist.setName("行业专家");
            specialist.setValue("ROLE_SPECIALIST");
            specialist.setAuthorities(Sets.newHashSet(authorityRepository.findById(1L).get()));
            roleRepository.save(specialist);

            OauthRole forumAdmin = new OauthRole();
            forumAdmin.setId(2L);
            forumAdmin.setName("版主");
            forumAdmin.setValue("ROLE_FORUM_ADMIN");
            forumAdmin.setAuthorities(Sets.newHashSet(authorityRepository.findById(2L).get()));
            roleRepository.save(forumAdmin);

            //商城
            OauthRole designer = new OauthRole();
            designer.setId(3L);
            designer.setName("设计师");
            designer.setValue("ROLE_DESIGNER");
            roleRepository.save(designer);

            OauthRole entrepreneur = new OauthRole();
            designer.setId(4L);
            entrepreneur.setName("企业认证");
            entrepreneur.setValue("ROLE_ENTREPRENEUR");
            roleRepository.save(entrepreneur);

            //======================================用户==========================================
            OauthUser fpf = new OauthUser();
            fpf.setUsername("flk");
            fpf.setPassword("flk");
            fpf.setPhone("11111111111");
            fpf.setOauthRoles(Sets.newHashSet(roleRepository.findById(1L).get()));
            userService.insertOauthUser(fpf);

            OauthUser wl = new OauthUser();
            wl.setUsername("wl");
            wl.setPassword("wl");
            wl.setOauthRoles(Sets.newHashSet(roleRepository.findById(2L).get()));
            userService.insertOauthUser(wl);
        }
    }
}
